package cc.fine.svc.main.service.impl.rel;

import cc.fine.svc.config.redis.utils.RedisCache;
import cc.fine.svc.main.bean.po.RelUserVideoFavor;
import cc.fine.svc.main.bean.po.TblUserCount;
import cc.fine.svc.main.mapper.RelUserVideoFavorMapper;
import cc.fine.svc.main.service.VideoCountService;
import cc.fine.svc.main.service.rel.RelUserVideoFavorService;
import cc.fine.svc.main.service.tbl.TblUserCountService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wyihe
 * @since 2023-10-25
 */
@Service
@Transactional
public class RelUserVideoFavorServiceImpl extends ServiceImpl<RelUserVideoFavorMapper, RelUserVideoFavor> implements RelUserVideoFavorService {

    @Autowired
    private VideoCountService videoCountService;

    @Autowired
    private TblUserCountService userCountService;

    @Autowired
    private RedisCache redisCache;

    @Override
    public void favor(RelUserVideoFavor favor) {
        QueryWrapper<RelUserVideoFavor> w = new QueryWrapper<>();
        w.eq("user_id", favor.getUserId());
        w.eq("video_id", favor.getVideoId());

        /**
         * 点过赞就不要再点了
         */
        if (getOne(w) == null) {
            videoCountService.increaseFavor(favor.getVideoId());
            save(favor);

            // 更新 userCount 表
            TblUserCount userCount = userCountService.getById(favor.getUserId());
            if (userCount == null) userCount = new TblUserCount();
            userCount.setFavors(userCount.getFavors() + 1);
            userCountService.saveOrUpdate(userCount);

            // 存储点赞
            List<String> videoIds = redisCache.getCacheMapValue(Favor_Cache, favor.getUserId());
            if (videoIds == null) videoIds = new ArrayList<>();
            videoIds.add(favor.getVideoId());
            redisCache.setCacheMapValue(Favor_Cache, favor.getUserId(), videoIds);
        }
    }

    @Override
    public void disFavor(RelUserVideoFavor favor) {
        QueryWrapper<RelUserVideoFavor> w = new QueryWrapper<>();
        w.eq("user_id", favor.getUserId());
        w.eq("video_id", favor.getVideoId());
        videoCountService.reduceFavor(favor.getVideoId());

        // 更新 userCount 表
        TblUserCount userCount = userCountService.getById(favor.getUserId());
        if (userCount == null) userCount = new TblUserCount();
        userCount.setFavors(userCount.getFavors() - 1);
        userCountService.saveOrUpdate(userCount);

        // 更新取消点赞
        List<String> videoIds = redisCache.getCacheMapValue(Favor_Cache, favor.getUserId());
        if (videoIds == null) videoIds = new ArrayList<>();
        videoIds.remove(favor.getVideoId());
        redisCache.setCacheMapValue(Favor_Cache, favor.getUserId(), videoIds);


        remove(w);
    }

    @Override
    public boolean isFavor(String userId, String videoId) {
        List<String> cacheMapValue = redisCache.getCacheMapValue(Favor_Cache, userId);
        if (cacheMapValue == null) return false;

        return cacheMapValue.contains(videoId);
    }
}
